package sites.manager.common.interceptor;

import com.jfinal.aop.Interceptor;
import com.jfinal.aop.Invocation;
import com.jfinal.core.Controller;
import sites.manager.common.kit.SiteInfo;
import sites.manager.common.model.Account;

import javax.servlet.http.HttpSession;

/**
 * 登录拦截器，未登录重定向至登录页面
 *
 * @author zyg
 * @date 2022/4/15 20:37
 */
public class LoggedInterceptor implements Interceptor {

    @Override
    public void intercept(Invocation invocation) {
        Controller controller = invocation.getController();
        HttpSession session = controller.getSession();
        Account account = (Account) session.getAttribute(SiteInfo.account);
        if(account == null){
            controller.redirect("/login");
        } else {
            invocation.invoke();
        }
    }

}
